/*
 *   Copyright 2008 Luca Bellonda <lbellonda@gmail.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as
 *   published by the Free Software Foundation; either version 2 or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "dbusappletItem.h"

#ifdef DUMP
#define   D(x)    x
#else
#define   D(x)
#endif
#include <stdio.h>

DBusAppletItem::DBusAppletItem()
{
    lastInserted = NULL ;
    D(printf("new DBusAppletItem"));
}

DBusAppletItem::DBusAppletItem(const DBusAppletItem &t)
{
    id = t.id ;
    idParent = t.idParent ;
    type = t.type ;
    data = t.data ;

    clear();

    int size = t.childs.size();
    for (int i = 0; i < size ; i++) {
        DBusAppletItem* child = t.childs.at(i);
        DBusAppletItem *lastOne = new DBusAppletItem(*child) ;
        D(if (NULL == lastOne)
          printf("NULL APPEND!\n"));
        childs.append(lastOne);
    }
    lastInserted = NULL ;
    D(printf("new DBusAppletItem copycc"));
}

DBusAppletItem& DBusAppletItem::operator=(const DBusAppletItem & t)
{

    if (this != &t) {
        clear();
        id = t.id ;
        idParent = t.idParent ;
        type = t.type ;
        data = t.data ;

        int size = t.childs.size();
        for (int i = 0; i < size ; i++) {
            DBusAppletItem* child = t.childs.at(i);
            DBusAppletItem *lastOne = new DBusAppletItem(*child) ;
            childs.append(lastOne);
        }
        lastInserted = NULL ;
    }
    return *this ;
}


DBusAppletItem::DBusAppletItem(const int p_id, const int p_idParent, const QString &p_type, const QString &p_value)
{
    id = p_id ;
    idParent = p_idParent ;
    type = p_type ;
    data = p_value ;
    lastInserted = NULL ;
    D(printf("new DBusAppletItem arg"));
}


DBusAppletItem::~DBusAppletItem()
{
    D(printf("predelete  DBusAppletItem\n"));
    clear();
    D(printf("delete  DBusAppletItem\n"));
}


void DBusAppletItem::clear()
{
    int size = childs.size();
    for (int i = 0; i < size ; i++) {
        DBusAppletItem* child = childs.at(i);
        delete child;
    }
    childs.clear();
}


QString DBusAppletItem::toString()
{
    return QString("%1|%2|%3|%4").arg(id).arg(idParent).arg(type).arg(data);
}


DBusAppletItem *DBusAppletItem::append(const int id, const int idParent, const QString &type, const QString &value)
{
    D(printf(" ADDING %s\n", QString("%1|%2|%3|%4").arg(id).arg(idParent).arg(type).arg(value)).toAscii().data());
    DBusAppletItem *lastOne = new DBusAppletItem(id, idParent, type, value) ;
    D(if (NULL == lastOne)
      printf("NULL APPEND!\n"));
    childs.append(lastOne);
    lastInserted = lastOne ;
    return lastOne ;
}

int DBusAppletItem::total()
{
    int i = 1;
    int size = childs.size();
    for (int i = 0; i < size ; i++) {
        DBusAppletItem* child = childs.at(i);
        i += child->total();
    }
    return i;
}

